package com.fzz.learning_forum.controller;

import com.fzz.learning_forum.domain.Article;
import com.fzz.learning_forum.domain.Comment;
import com.fzz.learning_forum.domain.User;
import com.fzz.learning_forum.service.AdminService;
import com.fzz.learning_forum.service.ArticleService;
import com.fzz.learning_forum.serviceImpl.AdminServiceImpl;
import com.fzz.learning_forum.serviceImpl.ArticleServiceImpl;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class AdminController {

    @Autowired
    private AdminService adminService=new AdminServiceImpl();

    @Autowired
    private ArticleService articleService=new ArticleServiceImpl();

    //注销管理员
    @RequestMapping(value = "/admin/logout")
    public String adminLogout(HttpSession session)
    {
        session.invalidate();
        return "redirect:/";
    }

    //查询所有用户
    @RequestMapping(value = "/admin/selectAllUser")
    public String selectAllUser(HttpServletRequest request)
    {
        getUserWithPage(request);
        return "back/user_list";
    }

    // 查询所有用户
    private void getUserWithPage(HttpServletRequest request) {
        // upage表示用户页数，userPage
        String upage = request.getParameter("upage");
        upage = StringUtils.isBlank(upage) ? "1" : upage;
        request.setAttribute("user", upage);
        PageInfo<User> userList = adminService.selectUserWithPage(Integer.parseInt(upage), 5);
        request.setAttribute("user", upage);
        request.setAttribute("userList", userList);
    }

    //查询所有动态
    @RequestMapping(value = "/admin/selectAllArticle")
    public String selectAllArticle(HttpServletRequest request)
    {
        getArticleWithPage(request);
        getUserWithPage(request);
        return "back/article_list";
    }

    // 查询所有文章
    private void getArticleWithPage(HttpServletRequest request) {
        // apage表示动态页数，articlePage
        String apage = request.getParameter("apage");
        apage = StringUtils.isBlank(apage) ? "1" : apage;
        request.setAttribute("article", apage);
        PageInfo<Article> articleList = articleService.selectArticleWithPage(Integer.parseInt(apage), 5);
        request.setAttribute("article", apage);
        request.setAttribute("articleList", articleList);
    }

    //通过id禁用用户
    @RequestMapping(value = "/admin/deleteUserById")
    @ResponseBody
    public String deleteUserById(Integer id)
    {
        try {
            Integer rows = adminService.deleteUserById(id);
            if(rows>0)
            {
                return "OK";
            }
        }
        catch (Exception e)
        {
            System.out.println(e.getCause());
        }
        return "NO";
    }

    //通过id授权用户
    @RequestMapping(value = "/admin/empowerUserById")
    @ResponseBody
    public String empowerUserById(Integer id)
    {
        try {
            Integer rows = adminService.empowerUserById(id);
            if(rows>0)
            {
                return "OK";
            }
        }
        catch (Exception e)
        {
            System.out.println(e.getCause());
        }
        return "NO";
    }

    //通过id禁用动态
    @RequestMapping(value = "/admin/deleteArticleById")
    @ResponseBody
    public String deleteArticleById(Integer id)
    {
        try {
            Integer rows = adminService.deleteArticleById(id);
            if(rows>0)
            {
                return "OK";
            }
        }
        catch (Exception e)
        {
            System.out.println(e.getCause());
        }
        return "NO";
    }

    //通过id授权用户
    @RequestMapping(value = "/admin/empowerArticleById")
    @ResponseBody
    public String empowerArticleById(Integer id)
    {
        try {
            Integer rows = adminService.empowerArticleById(id);
            if(rows>0)
            {
                return "OK";
            }
        }
        catch (Exception e)
        {
            System.out.println(e.getCause());
        }
        return "NO";
    }
}
